package com.sky.interceptor;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.utils.Constant;
import com.sky.utils.StringUtils;
import com.sky.utils.ThreadLocalUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;

@Component
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String method = request.getMethod();
        if (HttpMethod.GET.name().equals(method)){
            String pageNumParam = request.getParameter(Constant.PAGE_NUM);
            String pageSizeParam = request.getParameter(Constant.PAGE_SIZE);
            long pageNum = Long.parseLong(Optional.ofNullable(pageNumParam).orElse("1"));
            ThreadLocalUtils.pageNumThreadLocal.set(pageNum);

            long pageSize = Long.parseLong(Optional.ofNullable(pageSizeParam).orElse("10"));
            ThreadLocalUtils.pageSizeThreadLocal.set(pageSize);
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
